package com.meilele.controller.zhengchao;

import com.meilele.biz.zhengchao.ImageBiz;
import com.meilele.entity.zhengchao.AjaxMsgModel;
import com.meilele.entity.zhengchao.FourIndexImage;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 首页轮播图
 */
@Controller
@RequestMapping("/indexImage")
public class IndexImageController {
    @Resource
    private ImageBiz biz;
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<FourIndexImage> list=biz.findAll();
        model.addAttribute("list",list);
        return "four/manager/indexImg";
    }
    @ResponseBody
    @RequestMapping("/updateById")
    public AjaxMsgModel updateById(int id, MultipartHttpServletRequest request){
        AjaxMsgModel aa=null;
        try{
        FourIndexImage indexImage=new FourIndexImage();
        MultipartFile photo = request.getFile("photo");
        String fileNa = UUID.randomUUID().toString();
        String path= Contants.iINDEX_IMAGE_PATH + fileNa;
        File newFile = new File(path);
        try {
            photo.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        indexImage.setIndexImageId(id);
        indexImage.setIndexImage(fileNa);
        biz.updateById(indexImage);
        /*out.write("success");*/
       aa= new AjaxMsgModel("200","更换成功","articleImg","","closeCurrent","");
       return aa;
        }catch (Exception e){
            /*out.write("fail");*/
        }
        return aa;
    }
    @ResponseBody
    @RequestMapping("/updateRank")
    public AjaxMsgModel updateRank(String rank){
        AjaxMsgModel aa=null;
        try {
            biz.updateRank(rank);
            aa= new AjaxMsgModel("200","更换顺序成功","articleImg","","closeCurrent","");
            return aa;
        } catch (Exception e) {
           /* e.printStackTrace();*/
        }
        return aa;
    }
    @RequestMapping("upload")
    public String upload(){
        return "four/manager/upload";
    }
    @ResponseBody
    @RequestMapping("/deleteBeath")
    public AjaxMsgModel deleteBeath(int[] ids){
        biz.deleteBeach(ids);
        return new AjaxMsgModel("200","删除成功","artilce","","closeCurrent","");
    }
}
